package mysql

import (
	"context"

	"gitee.com/lyuanbo/stock-trade/internal/conf"
	"moul.io/zapgorm2"

	"go.uber.org/zap"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Option func(*gorm.Config)

func New(conf *conf.Conf, log *zap.Logger) *gorm.DB {
	logger := zapgorm2.New(log)
	logger.SetAsDefault()

	gormDb, err := gorm.Open(mysql.Open("root:123456@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"))
	if err != nil {
		logger.Error(context.TODO(), err.Error())
		panic(err)
	}

	db, err := gormDb.DB()
	err = db.Ping()
	if err != nil {
		logger.Error(context.TODO(), err.Error())
		panic(err)
	}
	return gormDb
}
